/*
package study.仪器解析.串口化通信.生化仪.监听COM1;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.example.biochemical.analyzer.entity.TotalRecord;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;

import java.util.List;

*/
/**
 * @Author 刘伟涛
 * @Date 2024/8/7 15:03
 **//*

public class SerialPortReader02 {
    private SerialPort serialPort;
    private StringBuffer receivedDataStringBuffer = new StringBuffer();

    public SerialPortReader02(String portName) {
        serialPort = new SerialPort(portName);
    }

    public void initialize() {
        try {
            // 打开串口
            serialPort.openPort();

            // 设置串口参数
            serialPort.setParams(
                    SerialPort.BAUDRATE_9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE
            );


            // 设置事件监听器，监听串口数据到达事件
            serialPort.addEventListener(new SerialPortEventListener() {
                @Override
                public void serialEvent(SerialPortEvent event) {
                    if (event.isRXCHAR()) { // 检查是否有数据到达
                            try {
                                //响应ACK，说明接收到了
                                char dataToSend = 6;
                                serialPort.writeByte((byte)dataToSend);
                                String receivedData = serialPort.readString(event.getEventValue());
                                System.out.println("receivedData:"+receivedData.replaceAll("[\\n\\r]",""));
                                if(!receivedData.contains(Character.toString('\u0004'))&&receivedData.trim().length() == 0){
                                    return;
                                }
                                receivedDataStringBuffer.append(receivedData);
                                if(receivedData.contains(Character.toString('\u0004'))){
                                    System.out.println("接收数据完成");
                                    ASTMParser astmParser = new ASTMParser();
                                    List<TotalRecord> totalRecords = astmParser.parseASTMMessage(receivedDataStringBuffer.toString());

                                    String json = JSONUtil.parseArray(totalRecords).toString();
                                    String url = "http://192.168.11.6:80/api/data/receive";
                                    String result = HttpRequest.post(url)
                                            .body(json)
                                            .execute().body();
                                    System.out.println("发送结果："+result);
                                    receivedDataStringBuffer.setLength(0);
                                }else {
                                    //回传太快，消息会丢失
                                    Thread.sleep(100);

                                }


                            } catch (SerialPortException | InterruptedException ex) {
                                System.out.println("Error in receiving data: " + ex);
                            }

                    }
                }
            });

        } catch (SerialPortException ex) {
            System.out.println("Error in initializing serial port: " + ex);
        }
    }

    public void close() {
        try {
            if (serialPort.isOpened()) {
                serialPort.closePort();
            }
        } catch (SerialPortException ex) {
            System.out.println("Error in closing serial port: " + ex);
        }
    }
}
*/
